/**
 * Created by zhaojm on 15/3/24.
 */
frame.BaseRole = cc.Class.extend({

    _road_x_list : [
        70,
        180,
        290
    ],

    _speedZ : 200,    // 沿着路方向的速度

    _lengthZ : 0,   // 路方向移动的距离
    _angleYZ : 75,    // 路方向和 视角方向的夹角
    _lengthY : 0,   // 沿着屏幕向上的路程，y坐标


    _angleXY : 89,  // 视角的大小
    _defaultContentSize : null,




    _layer:null,
    _node : null,
    _spriteSheet : null,

    _className : null,

    _currentRoad : null,    // 当前的路 0， 1， 2

    ctor:function(){
        this._node = this.getNode();
        this._defaultContentSize = this._node.getContentSize();

        this._node.setAnchorPoint(cc.p(0.5, 0));

        this._currentRoad = this.getRandomCurrentRoad();



    },

    getNode : function(){
        cc.log('need to be overwrite');
    },

    getNodeHeight : function(){
        cc.log('need to be overwrite');
    },

    doCollidedWithHero : function(){
        cc.log('need to be overwrite');
    },

    doCollidedWithBear:function(){
        cc.log('need to be overwrite');
    },


    update:function(dt){
        this._lengthZ += this._speedZ * dt;
        this._lengthY = Math.cos(this._angleYZ) * this._lengthZ;
        // y 坐标
        this._node.y = this._lengthY;

        // x坐标
        if(this._currentRoad == 0){
            this._node.x = this._road_x_list[this._currentRoad] + this._node.y / 420 * 105;
        }else if(this._currentRoad == 1){
            //this._node.x = this._road_x_list[this._currentRoad];
        }else if(this._currentRoad == 2){
            this._node.x = this._road_x_list[this._currentRoad] - this._node.y /  420 * 105;
        }


        // 缩放
        this._node.setScale((450 - this._node.y) / 450);


        // 和hero碰撞
        if(( this._lengthY + this.getNodeHeight() / 2 >= cc.winSize.height * 0.45 ) &&
            ( this._lengthY  - this.getNodeHeight() / 2  <= cc.winSize.height * 0.45 ) ){
            // TODO 检查和hero是否在同一跑道
            if((this._className == 'game.Wood' || this._currentRoad == this._layer._hero._currentRoad) &&
                this._layer._hero._stateTag != this._layer._hero._stateTag_enum.jump){
                // TODO 播放动画，消失，移除，加分
                this.stopUpdate();
                this.doCollidedWithHero();
                return;
            }

        }

        // 到了路尽头
        if(this._lengthY + this.getNodeHeight() / 2 >= 440 / 600 * cc.winSize.height){
            this.stopUpdate();
            this.removeFromLayer();
            return;
        }

        // TODO  检查 是否和 bear碰撞了，检查是否到达bear的位置
        // TODO 碰撞了，提速
        if(this._layer._bear.getScale() > 0 && this._lengthY + this.getNodeHeight() / 2 >= this._layer._bear.getCollidedHeight()){
            this.stopUpdate();
            this.doCollidedWithBear();
            return;
        }

    },

    getRandomCurrentRoad:function(){
       return  Math.floor(Math.random() * 3);
    },


    addToLayer : function(layer){
        this._layer = layer;

        layer.addChild(this._node);
        this._node.setPosition(this._road_x_list[this._currentRoad], 0);
        layer._objList.push(this);

    },

    removeFromLayer : function(){
        this._layer.removeChild(this._node);
    },

    stopUpdate : function(){
        for (var i = 0; i < this._layer._objList.length;  i++) {
            if (this._layer._objList[i] == this) {
                this._layer._objList.splice(i, 1);
                break;
            }
        }
    },




});